using System;
using System.Text;
using System.Reflection;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace EntLibContrib.PolicyInjection
{
    [ConfigurationElementType(typeof(AndMatchingRuleData))]
    public class AndMatchingRule : IMatchingRule
    {
        List<IMatchingRule> rules;

        public AndMatchingRule(IEnumerable<IMatchingRule> rules)
        {
            if (rules == null) throw new ArgumentNullException("rules");

            this.rules = new List<IMatchingRule> (rules);
        }

        #region IMatchingRule Members

        public bool Matches(MethodBase member)
        {
            if (rules.Count == 0) return true;
            foreach (IMatchingRule rule in rules)
            {
                if (!rule.Matches(member))
                {
                    return false;
                }
            }
            return true;
        }

        #endregion
    }
}
